/**
 * @author:cavion(曹仁道)
 * @email:caorendao187@163.com
 * 2018年3月4日
 */
package org.rk.core.nosql.mongodb.base;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.rk.core.common.exception.RkMsgException;
import org.rk.core.common.util.RkObjectUtil;
import org.rk.core.common.util.RkStrUtil;
import org.rk.core.nosql.mongodb.baseBean.NoSqlCondition;
import org.rk.core.nosql.mongodb.baseBean.NoSqlParamMap;
import org.springframework.data.mongodb.core.query.Query;

/**
 * @author cavion
 * @描述
 * 2018年3月4日 上午11:17:43
 */
public class BaseMongoServiceImpl<T> implements IBaseMongoService<T> {
	
	private IBaseMongoDao<T> baseMogoDao;

	public IBaseMongoDao<T> getBaseMogoDao() {
		return baseMogoDao;
	}

	public void setBaseMogoDao(IBaseMongoDao<T> baseMogoDao) {
		this.baseMogoDao = baseMogoDao;
	}
	@Override
	public void insert(T t) {
		baseMogoDao.insert(t);
	}
	@Override
	public T findById(String id) {
		return baseMogoDao.findOne(id);
	}

	@Override
	public List<T> findLtByCreateTime(Date createTime) {
		List<NoSqlParamMap> paramMapList = new ArrayList<NoSqlParamMap>();
		paramMapList.add(new NoSqlParamMap(NoSqlCondition.condi_mode_lt,NoSqlCondition.condi_type_and,"createTime",createTime));
		return findByParamMap(paramMapList);
	}

	@Override
	public List<T> findGtByCreateTime(Date createTime) {
		List<NoSqlParamMap> paramMapList = new ArrayList<NoSqlParamMap>();
		paramMapList.add(new NoSqlParamMap(NoSqlCondition.condi_mode_gt,NoSqlCondition.condi_type_and,"createTime",createTime));
		return findByParamMap(paramMapList);
	}

	@Override
	public List<T> findGtLtByCreateTime(Date createTimeStart, Date createTimeEnd) {
		List<NoSqlParamMap> paramMapList = new ArrayList<NoSqlParamMap>();
		paramMapList.add(new NoSqlParamMap(NoSqlCondition.condi_mode_gt_eq,NoSqlCondition.condi_type_and,"createTime",createTimeStart));
		paramMapList.add(new NoSqlParamMap(NoSqlCondition.condi_mode_lt_eq,NoSqlCondition.condi_type_and,"createTime",createTimeEnd));
		return findByParamMap(paramMapList);
	}
	
	@Override
	public List<T> findByProperty(String propName,Object propObj){
		if(RkStrUtil.isEmpty(propName)) {
			throw new RkMsgException("属性名称不可为空");
		}
		List<NoSqlParamMap> paramMapList = new ArrayList<NoSqlParamMap>();
		paramMapList.add(new NoSqlParamMap(NoSqlCondition.condi_mode_eq,NoSqlCondition.condi_type_and, propName ,propObj));
		return findByParamMap(paramMapList);
	}

	@Override
	public List<T> find(Query query) {
		return baseMogoDao.find(query);
	}
	@Override
	public List<T> findByParamMap(List<NoSqlParamMap> paramMapList){
		if(RkObjectUtil.isEmpty(paramMapList) || paramMapList.size()<1) {
			return baseMogoDao.find(new Query());
		}
		Query query = new Query();
		query.addCriteria(NoSqlCondition.genCriteria(paramMapList,false));
		return baseMogoDao.find(query);
	}
	
	

}
